<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Exception;

/**
 * Class ContentController
 * @package Admin\Controller
 */
class ContentController extends Controller {

    /**
     * 文章管理首页
     */
    public function index()
    {
        $condition = [];
        if (I('title')){
            $condition['title'] = I('title');
        }
        $this->assign('title' , I('title'));
        if (I('catid')){
            $condition['carid'] = intval(I('catid'));
        }
        $page = $_REQUEST['p'] ? $_REQUEST['p'] : 1 ;
        $pageSize = 3;
        $condition['status'] = ['neq' , -1];
        $news = D('News')->getNews($condition , $page , $pageSize);
        $newCount = D('News')->getNewsCount($condition);
        $res = new \Think\Page($newCount , $pageSize);
        $pageRes = $res->show();
        /*推荐位*/
        $positions = D('Position')->getNormalPositions();
        $this->assign('pageres' , $pageRes);
        $this->assign('positions' , $positions);
        $this->assign('news' , $news);
        $this->assign('websites' , D('Menu')->getBarMenus());
        $this->display();
    }

    /**
     * 文章添加
     */
    public function add()
    {
        if (IS_POST){
            if ( !isset($_POST['title']) || !$_POST['title']){
                return show(0, '标题不能为空');
            }
            if ( !isset($_POST['small_title']) || !$_POST['small_title']){
                return show(0, '短标题不能为空');
            }
            if ( !isset($_POST['catid']) || !$_POST['catid']){
                return show(0, '栏目不能为空');
            }
            if ( !isset($_POST['content']) || !$_POST['content']){
                return show(0, '内容不能为空');
            }
            if ( !isset($_POST['keywords']) || !$_POST['keywords']){
                return show(0, '关键字不能为空');
            }
            /*将标题内容插入主表*/
            $newId = D('News')->insert(I('post.'));
            if ($newId){
                $newsCententData['content'] = I('content');
                $newsCententData['news_id'] = $newId;
                /*将文章内容加入到附表*/
                $cId = D('NewsContent')->insert($newsCententData);
                if (!empty($cId)){
                    return show(1, '插入成功');
                }else{
                    return show(1, '主表插入成功 ， 附表插入失败' );
                }
            }else{
                return show(0, '插入失败 , 请重试');
            }
        }else{
            $webSiteMenu = D('Menu')->getBarMenus();
            $titleFontColor = C("TITLE_FONT_COLOR");
            $copyFrom = C("COPY_FROM");
            $this->assign('webSiteMenu' , $webSiteMenu);
            $this->assign('titleFontColor' , $titleFontColor);
            $this->assign('copyFrom' , $copyFrom);
            $this->display();
        }
    }
    /**
     * 文章编辑
     */
    public function edit()
    {
        $id = I('id');
        if (!$id){
            $this->redirect('/admin.php?c=content');
        }
        $this->assign('news' , D('News')->find($id));
        $this->assign('copyFrom' , C('COPY_FROM'));
        $this->assign('titleFontColor' , C('TITLE_FONT_COLOR'));
        $this->assign('websites' , D('Menu')->getBarMenus());
        $this->assign('newscontent' , D('NewsContent')->where('news_id=' . $id)->find());
        $this->display();
    }

    /**
     * 文章编辑
     */
    public function save()
    {
        $id = I('news_id');
        $content['content'] = I('content');
        $data = I('post.');
        try{
            $getId = D('News')->updateById($id , $data);
            $getContentId = D('NewsContent')->updateNewsById($id , $content);
            if ($getId === false || $getContentId === false){
                return show(0, "更新失败");
            }
            return show(1, "更新成功");
        }catch (Exception $e){
            return show(0, $e->getMessage());
        }
    }

    /**
     * 文章删除【修改状态】
     */
    public function setStatus()
    {
        try{
            if (IS_POST){
                $id = I('id');
                $status = I('status');
                if (!$id){
                    return show(0, "id不存在");
                }
                $res = D("News")->updateStatusById($id , $status);
                if ($res){
                    return show(1, "操作成功");
                }else{
                    return show(0, '操作失败');
                }
            }
            return show(0, '没有提交内容');
        }catch (Exception $e){
            return show(0, $e->getMessage());
        }
    }

    /**
     * 文章排序
     */
    public function listorder()
    {
        $listorder = I('listorder');
        $jumpUrl = $_SERVER['HTTP_REFERER'];
        $error = [];
        try {
            foreach ($listorder as $id => $item) {
                //执行修改操作
                $id = D("News")->updateListorderById($id, $item);
                if ($id === false) {
                    $error[] = $id;
                }
            }
            if ($error) {
                return show(0, '排序失败~' . implode(',', $error), ['jump_url' => $jumpUrl]);
            }
            return show(1, '排序成功', ['jump_url' => $jumpUrl]);
        }catch (Exception $e){
            return show(0, $e->getMessage());
        }
        return show(0, '排序数据出错 ， 请重试');
    }

    /**
     * 推荐位管理
     */
    public function push() {
        $JumpUrl = $_SERVER['HTTP_REFERER'];
        $positionId = I('position_id');
        $newsId = I('push');
        //判断传来的数据是否合法
        if (!$newsId || !is_array($newsId)){
            return show(0 , "请选择推荐文章的ID进行推荐");
        }
        if (!$positionId){
            return show(0 , "没有推荐位");
        }
        try {
            //获取文章的数据
            $news = D("News")->getNewsByIdIn($newsId);
            if (!$news) {
                return show(0, "没哟相关的内容");
            }
            $data = [];
            $data['status'] = 1;
            $data['position_id'] = $positionId;
            foreach ($news as $new) {
                $data['title'] = $new['title'];
                $data['thumb'] = $new['thumb'];
                $data['news_id'] = $new['news_id'];
                $data['create_time'] = time();
            }
            $position = D('PositionContent')->insert($data);
        }catch (Exception $e){
            return show(0,$e->getMessage());
        }
        return show(1 , "推荐成功" , ['jump_url' => $JumpUrl]);

    }





























}